package com.xdjd.mobile.system.config;

import org.apache.commons.lang.math.NumberUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * <pre>
 * 
 * </pre>
 * 
 * @author tanghc
 *
 */
@Component
public class JedisConfig {

	@Value("${spring.redis.database}")
	private String database;
	@Value("${spring.redis.host}")
	private String host;
	@Value("${spring.redis.password}")
	private String password;
	@Value("${spring.redis.port}")
	private String port;
	@Value("${spring.redis.timeout}")
	private String timeout;
	
	@Value("${spring.redis.pool.max-idle}")
	private String maxIdle;
	@Value("${spring.redis.pool.min-idle}")
	private String minIdle;
	@Value("${spring.redis.pool.max-active}")
	private String maxActive;
	@Value("${spring.redis.pool.max-wait}")
	private String maxWait;

	@Bean
	public JedisPool jedisPool() {
		JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
		jedisPoolConfig.setMaxIdle(NumberUtils.toInt(maxIdle, JedisPoolConfig.DEFAULT_MAX_IDLE));
		jedisPoolConfig.setMinIdle(NumberUtils.toInt(minIdle, JedisPoolConfig.DEFAULT_MIN_IDLE));
		jedisPoolConfig.setMaxTotal(NumberUtils.toInt(maxActive, JedisPoolConfig.DEFAULT_MAX_TOTAL));
		jedisPoolConfig.setMaxWaitMillis(NumberUtils.toLong(maxWait, JedisPoolConfig.DEFAULT_MAX_WAIT_MILLIS));
		jedisPoolConfig.setTestOnBorrow(true);
		jedisPoolConfig.setTestOnReturn(true);
		
		return new JedisPool(jedisPoolConfig, 
				host, 
				NumberUtils.toInt(port, 6379),
				NumberUtils.toInt(timeout, 3000),
				password,
				NumberUtils.toInt(database, 0));
	}

}
